package com.cpit.platform.web.controller;

import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dto.MainPageItem;
import com.cpit.platform.dto.tiles.newscard.NewsCard;
import com.cpit.platform.dto.tiles.numercard.NumberCard;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.MainPageService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/mainpageitem")
public class MainPageItemController {

	@Autowired
	private MainPageService MainPageItemService;
	
	private final String loggerName = this.getClass().getName();
	
	@ApiOperation(value="查询首页配置项", notes="通过条件查询页配置项" + 
	"支持分页查询，支持模糊查询。")
	@ResponseBody
	@PreAuthorize("hasAuthority('queryMainPageItem')")
	@RequestMapping(value="/queryMainPageItem", method = RequestMethod.GET)  
	public List<MainPageItem> queryMainPageItem(
			@ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = true, defaultValue = "1") int page, 
			@ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = true, defaultValue = "20") int size, 
			@ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order, 
			@ApiParam(value = "排序字段，与dto中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy, 
			@ApiParam(value = "模糊搜索的字符串，根据页配置项的名称字段进行模糊匹配。") @RequestParam(value = "sysName", required = false, defaultValue = "") String sysName)  {
		try {
			return MainPageItemService.findByCondition(page, size, order, orderBy, sysName);
		} catch(Exception e) {
			LogOperator.err(loggerName, "get main page item error", e);
			return null;
		}
	}  
	@ApiOperation(value="查询页配置项",notes="通过id查找页配置项")
	@ResponseBody
	@PreAuthorize("hasAuthority('queryMainPageItem')")
	@RequestMapping(value="/getMainPageItem",method = RequestMethod.GET)
	public MainPageItem getMainPageItem(
			@ApiParam(value = "搜索id，页配置项的id字段进行匹配。") @RequestParam(value = "itemId", required = false, defaultValue = "") Integer itemId)  {
		return MainPageItemService.findByID(itemId);
	}
	
	@ApiOperation(value="查询页配置项列表", notes="通过条件查询系统中的页配置项信息" + 
			"支持分页查询，支持模糊查询。")
		    @ApiImplicitParams({
		            @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
		    })
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@PreAuthorize("hasAuthority('queryMainPageItem')")
	@RequestMapping(value="/queryMainPageItemJqg", method = RequestMethod.GET)  
	public TableJsonData queryMainPageItemJqg(TableDataQueryCondition cond) {
		try{
			TableJsonData tjd = MainPageItemService.findByConditionJqg(cond);
			return tjd;
		} catch(Exception e) {
			LogOperator.err(loggerName, "get main page item error", e);
			return null;
		}
	}  
	
	@ApiOperation(value="创建页配置项", notes="用户手工指定页配置项并创建，" + 
			"需要手动输页配置项的详细信息，包括名称、描述，以生产厂商、系统类型、登录类型。")
		     @ApiImplicitParam(name = "data", value = "页配置项描述对象。", required = true, dataType = "MainPageItem")
	@ResponseBody
	@PreAuthorize("hasAuthority('createMainPageItem')") 
	@RequestMapping(value="/createMainPageItem", method = RequestMethod.POST)  
	public Result<MainPageItem> createMainPageItem(@RequestBody MainPageItem data) {
		try{
			MainPageItemService.create(data);
			return new Result<MainPageItem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<MainPageItem>(Result.CODE_FAILED, "", data);
		}
	}  
	
	@ApiOperation(value="修改页配置项", notes="用户手工指定页配置项并修改，" + 
			"需要手动输入页配置项的详细信息，包括名称、描述，以生产厂商、系统类型、登录类型。")
		     @ApiImplicitParam(name = "data", value = "页配置项描述对象。", required = true, dataType = "MainPageItem")
	@ResponseBody
	@PreAuthorize("hasAuthority('modifyMainPageItem')")
	@RequestMapping(value="/modifyMainPageItem", method = RequestMethod.POST)  
	public Result<MainPageItem> modifyMainPageItem(@RequestBody MainPageItem data) {
		try{
			MainPageItemService.create(data);
			return new Result<MainPageItem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<MainPageItem>(Result.CODE_FAILED, "", data);
		}
	}
	
	@ApiOperation(value="删除页配置项", notes="在系统中删除已经存在的页配置项，")
		     @ApiImplicitParam(name = "data", value = "页配置项描述对象。", required = true, dataType = "MainPageItem")
	@ResponseBody
	@PreAuthorize("hasAuthority('deleteMainPageItem')")
	@RequestMapping(value="/deleteMainPageItem", method = RequestMethod.DELETE)  
	public Result<MainPageItem> deleteMainPageItem(@RequestBody MainPageItem data) {
		try{
			MainPageItemService.delete(data);
			return new Result<MainPageItem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<MainPageItem>(Result.CODE_FAILED, "", data);
		}
	}
	
	@ApiOperation(value="测试数量统计卡生成代码", notes="测试数量统计卡生成代码")
	@ResponseBody
	@RequestMapping(value="/testNumberCard", method = RequestMethod.GET)  
	public NumberCard testNumberCard() {
		try{
			NumberCard nc = new NumberCard();
			nc.setName("任务提醒");
			nc.setCount(10);
			nc.setMoreUrl("http://www.sina.com/");
			List<com.cpit.platform.dto.tiles.numercard.Card> data = new ArrayList<com.cpit.platform.dto.tiles.numercard.Card>();
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("本日任务", "2", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("本周任务", "6", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("本月任务", "17", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("已完成任务", "9", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("紧急任务", "1", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("协助任务", "5", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("取消任务", "3", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("调度任务", "54", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("领导任务", "2", "http://www.baidu.com/"));
			data.add(new com.cpit.platform.dto.tiles.numercard.Card("删除任务", "2", "http://www.baidu.com/"));
			nc.setData(data);
			return nc;
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@ApiOperation(value="测试新闻卡生成代码", notes="测试新闻卡生成代码")
	@ResponseBody
	@RequestMapping(value="/testNewsCard", method = RequestMethod.GET)  
	public NewsCard testNewsCard() {
		try{
			NewsCard nc = new NewsCard();
			nc.setCardCount(5);
			List<com.cpit.platform.dto.tiles.newscard.Card> data = new ArrayList<com.cpit.platform.dto.tiles.newscard.Card>();
			nc.setData(data);
			com.cpit.platform.dto.tiles.newscard.Card n1 = new com.cpit.platform.dto.tiles.newscard.Card();
			data.add(n1);
			n1.setName("公司内新闻");
			n1.setCount("4");
			n1.setMoreUrl("http://www.youku.com/");
			List<com.cpit.platform.dto.tiles.newscard.News> news1 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
			n1.setNews(news1);
			news1.add(new com.cpit.platform.dto.tiles.newscard.News("中国信通院发布首个NB-IoT网络测试报告", "2017-08-11", "http://imis.rd.potevio.wps/portal"));
			news1.add(new com.cpit.platform.dto.tiles.newscard.News("爱立信，英特尔与NTT成立汽车大数据联盟", "2017-08-13", "http://imis.rd.potevio.wps/portal"));
			news1.add(new com.cpit.platform.dto.tiles.newscard.News("PPP覆盖30省市 制度规范成未来工作方向", "2017-08-14", "http://imis.rd.potevio.wps/portal"));
			news1.add(new com.cpit.platform.dto.tiles.newscard.News("普天技术举办人工智能专题培训", "2017-08-16", "http://imis.rd.potevio.wps/portal"));
			
			com.cpit.platform.dto.tiles.newscard.Card n2 = new com.cpit.platform.dto.tiles.newscard.Card();
			data.add(n2);
			n2.setName("外部新闻");
			n2.setCount("3");
			n2.setMoreUrl("http://www.tudou.com/");
			List<com.cpit.platform.dto.tiles.newscard.News> news2 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
			n2.setNews(news2);
			news2.add(new com.cpit.platform.dto.tiles.newscard.News("PPP覆盖30省市 制度规范成未来工作方向", "2017-08-14", "http://imis.rd.potevio.wps/portal"));
			news2.add(new com.cpit.platform.dto.tiles.newscard.News("将同舟共济的奋斗精神融入行动", "2017-08-15", "http://imis.rd.potevio.wps/portal"));
			news2.add(new com.cpit.platform.dto.tiles.newscard.News("普天技术举办人工智能专题培训", "2017-08-16", "http://imis.rd.potevio.wps/portal"));
			
			com.cpit.platform.dto.tiles.newscard.Card n3 = new com.cpit.platform.dto.tiles.newscard.Card();
			data.add(n3);
			n3.setName("国际新闻");
			n3.setCount("4");
			n3.setMoreUrl("http://www.iqiyi.com/");
			List<com.cpit.platform.dto.tiles.newscard.News> news3 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
			n3.setNews(news3);
			news3.add(new com.cpit.platform.dto.tiles.newscard.News("中国信通院发布首个NB-IoT网络测试报告", "2017-08-11", "http://imis.rd.potevio.wps/portal"));
			news3.add(new com.cpit.platform.dto.tiles.newscard.News("2022年前智慧城市市场规模将高速增长", "2017-08-12", "http://imis.rd.potevio.wps/portal"));
			news3.add(new com.cpit.platform.dto.tiles.newscard.News("爱立信，英特尔与NTT成立汽车大数据联盟", "2017-08-13", "http://imis.rd.potevio.wps/portal"));
			news3.add(new com.cpit.platform.dto.tiles.newscard.News("PPP覆盖30省市 制度规范成未来工作方向", "2017-08-14", "http://imis.rd.potevio.wps/portal"));
			
			com.cpit.platform.dto.tiles.newscard.Card n4 = new com.cpit.platform.dto.tiles.newscard.Card();
			data.add(n4);
			n4.setName("公司内新闻");
			n4.setCount("1");
			n4.setMoreUrl("http://www.tencent.com/");
			List<com.cpit.platform.dto.tiles.newscard.News> news4 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
			n4.setNews(news4);
			news4.add(new com.cpit.platform.dto.tiles.newscard.News("普天技术举办人工智能专题培训", "2017-08-16", "http://imis.rd.potevio.wps/portal"));
			
			com.cpit.platform.dto.tiles.newscard.Card n5 = new com.cpit.platform.dto.tiles.newscard.Card();
			data.add(n5);
			n5.setName("国内新闻");
			n5.setCount("4");
			n5.setMoreUrl("http://www.zhihu.com/");
			List<com.cpit.platform.dto.tiles.newscard.News> news5 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
			n5.setNews(news5);
			news5.add(new com.cpit.platform.dto.tiles.newscard.News("爱立信，英特尔与NTT成立汽车大数据联盟", "2017-08-13", "http://imis.rd.potevio.wps/portal"));
			news5.add(new com.cpit.platform.dto.tiles.newscard.News("PPP覆盖30省市 制度规范成未来工作方向", "2017-08-14", "http://imis.rd.potevio.wps/portal"));
			news5.add(new com.cpit.platform.dto.tiles.newscard.News("将同舟共济的奋斗精神融入行动", "2017-08-15", "http://imis.rd.potevio.wps/portal"));
			news5.add(new com.cpit.platform.dto.tiles.newscard.News("普天技术举办人工智能专题培训", "2017-08-16", "http://imis.rd.potevio.wps/portal"));
			
//			com.cpit.platform.dto.tiles.newscard.Card n6 = new com.cpit.platform.dto.tiles.newscard.Card();
//			data.add(n6);
//			n6.setName("政务消息");
//			n6.setCount("6");
//			n6.setMoreUrl("http://www.qq.com/");
//			List<com.cpit.platform.dto.tiles.newscard.News> news6 = new ArrayList<com.cpit.platform.dto.tiles.newscard.News>();
//			n6.setNews(news6);
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("PPP覆盖30省市 制度规范成未来工作方向", "2017-08-14", "http://imis.rd.potevio.wps/portal"));
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("将同舟共济的奋斗精神融入行动", "2017-08-15", "http://imis.rd.potevio.wps/portal"));
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("普天技术举办人工智能专题培训", "2017-08-16", "http://imis.rd.potevio.wps/portal"));
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("中国信通院发布首个NB-IoT网络测试报告", "2017-08-11", "http://imis.rd.potevio.wps/portal"));
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("2022年前智慧城市市场规模将高速增长", "2017-08-12", "http://imis.rd.potevio.wps/portal"));
//			news6.add(new com.cpit.platform.dto.tiles.newscard.News("爱立信，英特尔与NTT成立汽车大数据联盟", "2017-08-13", "http://imis.rd.potevio.wps/portal"));
			return nc;
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
